package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.DiBoundEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.DiBoundRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class DiBoundRepositoryImpl implements DiBoundRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveDiBound(DiBoundEntity it) {
        jdbcTemplate.update("insert into czt_damage_index_bound(cdib_id,grade,cdib_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCdibId(), it.getGrade(), it.getCdibValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updateDiBound(DiBoundEntity it) {
        String sql = "update czt_damage_index_bound set grade= :grade,cdib_value= :cdibValue,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where cdib_id= '"+it.getCdibId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<DiBoundEntity> getAllDiBound() {
        String sql = "select * from czt_damage_index_bound where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DiBoundEntity.class));
    }

    @Override
    public void deleteDiBoundById(String diBoundId) {
        String sql = "update czt_damage_index_bound set del_flag = '1' where cdib_id=?";
        jdbcTemplate.update(sql, diBoundId);
    }

    @Override
    public void deleteDiBoundByProjectId(String arrangeId) {
        String sql = "update czt_damage_index_bound set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }
}
